﻿using System;
using System.Web;
using System.Linq;using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using WebHttpApi.WebHttpApi.Core;
using System.IO;
using System.Globalization;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// ZhPostData 的摘要说明：珠华平台数据接入处理类
    /// </summary>
    public class ZhPostData
    {
        public static string result(Stream postData)
        {
            string str_result = "";

            if (postData.Length != 0)
            {

                StreamReader sRead = new StreamReader(postData);
                string postContent = sRead.ReadToEnd();

                sRead.Close();
                JObject json_post = (JObject)JsonConvert.DeserializeObject(postContent);
                string str_post_json = ZhSign.str_json(json_post);

                string str_sign = "";
                str_sign = json_post["sign"].ToString();
                
                if (WebSdkCore.GetMD5String(str_post_json) == str_sign)
                {
                    string str_client_code = "";
                    string str_meter_number = "";
                    string str_value = "";
                    string str_upload_time = "";
                    string str_valve_status = "";

                    //取值
                    foreach (JToken child in json_post.Children())
                    {
                        var temp = child as JProperty;

                        switch (temp.Name)
                        {
                            case "client_code":
                                {
                                    str_client_code = temp.Value.ToString();
                                    break;
                                }

                            case "meter_number":
                                {
                                    str_meter_number = temp.Value.ToString();
                                    break;
                                }

                            case "value":
                                {
                                    str_value = temp.Value.ToString();
                                    break;
                                }

                            case "upload_time":
                                {
                                    str_upload_time = Convert.ToDateTime(temp.Value.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                                    break;
                                }

                            case "valve_status":
                                {
                                    str_valve_status = temp.Value.ToString();

                                    if (str_valve_status == "0")
                                    {
                                        str_valve_status = "关";
                                    }
                                    else if (str_valve_status == "1")
                                    {
                                        str_valve_status = "开";
                                    }
                                    else if (str_valve_status == "2")
                                    {
                                        str_valve_status = "未上报";
                                    }
                                    else
                                    {
                                        str_valve_status = "异常";
                                    }
                                    break;
                                }
                        }
                    }

                    if (SelectIn(str_meter_number,str_value,str_upload_time))
                    {
                        DateTime str_system_time = DateTime.Now;

                        string sql = "select ybdz from rp_meter_jk where ybdz='" +
                                      str_meter_number + "'";

                        System.Data.DataTable Meter_Data = WebSdkCore.select(sql);

                        if (Meter_Data.Rows.Count > 0)
                        {
                            bool operatorlog = WebSdkCore.insert_update_delete("update rp_meter_jk set zxlj='" +
                                                                                str_value + "',txsj='" +
                                                                                str_system_time + "',ybsj='" +
                                                                                str_upload_time + "',sbsj='" +
                                                                                str_upload_time + "',fmzt='" +
                                                                                str_valve_status + "' where ybdz='" +
                                                                                str_meter_number + "'");

                            if (operatorlog == true)
                            {
                                operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_sj(ybdz,sbdz,zxlj,txsj,ybsj,sbsj,fmzt) values ('" +
                                                       str_meter_number + "','" +
                                                       str_meter_number + "','" +
                                                       str_value + "','" +
                                                       str_system_time + "','" +
                                                       str_upload_time + "','" +
                                                       str_upload_time + "','" +
                                                       str_valve_status + "')");

                                if (operatorlog == true)
                                {
                                    var json_result = new
                                    {
                                        status_code = "200",
                                        message = "ok",
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                                else
                                {
                                    var json_result = new
                                    {
                                        status_code = "500",
                                        message = "数据写入失败",
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                            }
                            else
                            {
                                var json_result = new
                                {
                                    status_code = "500",
                                    message = "数据写入失败",
                                };
                                str_result = WebSdkCore.ToJSON(json_result);
                            }
                        }
                        else if (Meter_Data.Rows.Count == 0)
                        {
                            bool operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_jk(ybdz,sbdz,zxlj,txsj,ybsj,sbsj,fmzt) values ('" +
                                                   str_meter_number + "','" +
                                                   str_meter_number + "','" +
                                                   str_value + "','" +
                                                   str_system_time + "','" +
                                                   str_upload_time + "','" +
                                                   str_upload_time + "','" +
                                                   str_valve_status + "')");

                            if (operatorlog == true)
                            {
                                operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_sj(ybdz,sbdz,zxlj,txsj,ybsj,sbsj,fmzt) values ('" +
                                                       str_meter_number + "','" +
                                                       str_meter_number + "','" +
                                                       str_value + "','" +
                                                       str_system_time + "','" +
                                                       str_upload_time + "','" +
                                                       str_upload_time + "','" +
                                                       str_valve_status + "')");

                                if (operatorlog == true)
                                {
                                    var json_result = new
                                    {
                                        status_code = "200",
                                        message = "ok",
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                                else
                                {
                                    var json_result = new
                                    {
                                        status_code = "500",
                                        message = "数据写入失败",
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                            }
                            else
                            {
                                var json_result = new
                                {
                                    status_code = "500",
                                    message = "数据写入失败",
                                };
                                str_result = WebSdkCore.ToJSON(json_result);
                            }
                        }
                        else
                        {
                            var json_result = new
                            {
                                status_code = "500",
                                message = "数据写入失败",
                            };
                            str_result = WebSdkCore.ToJSON(json_result);
                        }
                    }
                    else
                    {
                        //参数错误
                        var json_result = new
                        {
                            status_code = "422",
                            message = "参数错误",
                        };
                        str_result = WebSdkCore.ToJSON(json_result);
                    }
                }
                else
                {
                    var json_result = new
                    {
                        status_code = "401",
                        message = "签名错误",
                    };
                    str_result = WebSdkCore.ToJSON(json_result);
                }

            }
            else
            {
                //参数为空
                var json_result = new
                {
                    status_code = "422",
                    message = "参数错误",
                };
                str_result = WebSdkCore.ToJSON(json_result);
            }
            return str_result;
        }

        public static bool SelectIn(string meter_number, string value, string upload_time)
        {
            bool result;
            if (string.IsNullOrEmpty(meter_number) || string.IsNullOrEmpty(value) || string.IsNullOrEmpty(upload_time))
            {
                result = false;
            }
            else
            {
                result = true;
            }
            return result;
        }
    }
}
